package com.msmall.community.wiki.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.model.ArticleReply;
import com.msmall.community.klg.util.KLGUtil;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.model.UserSetting;
import com.msmall.community.user.service.UserManager;
import com.msmall.community.wiki.dao.WikiDao;
import com.msmall.community.wiki.model.WikiSegmentArticle;
import com.msmall.community.wiki.service.WikiManager;

public class WikiManagerImpl extends BaseManagerImpl implements WikiManager{
	
	private static final Log log = LogFactory.getLog(WikiManagerImpl.class);
	
	private WikiDao wikiDao;
	
	private UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public void setWikiDao(WikiDao wikiDao) {
		this.wikiDao = wikiDao;
	}
	/**
	 * 通过字典/百科所属于的分类找出对应的文章
	 */
	public Article getBySegmentid(Long segid) throws Exception{
		try{
			List<Article> list = wikiDao.getBySegmentid(segid);
			return list.get(0);
		}catch(Exception e){
			log.debug(e);
			throw e;
		}
	}
	public WikiSegmentArticle getByArticleid(Long articleid) throws Exception{
		try{
			WikiSegmentArticle wsa = wikiDao.getByArticleid(articleid);
			return wsa;
		}catch(Exception e){
			log.debug(e);
			throw e;
		}
	}
	/**
	 * 用户是否被授权推荐问答
	 */
	public boolean isAuthorizedCommend(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_COMMEND_WIKI);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_COMMEND_WIKI_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 用户是否被授权删除问答/回复
	 */
	public boolean isAuthorizedRemove(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_REMOVE_WIKI);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_REMOVE_WIKI_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 用户是否被授权创建问答/回复
	 */
	public boolean isAuthorizedCreate(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_CREATE_WIKI);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_CREATE_WIKI_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	
	/**
	 * 用户是否被授权更新问答/回复
	 */
	public boolean isAuthorizedUpdate(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_UPDATE_WIKI);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_UPDATE_WIKI_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 创建新的WIKI文章
	 * @param article
	 * @param segid 文章分类ID
	 * @param userid creator
	 * @param userAgentType
	 * @return
	 */
	public Article saveWiki(Article article, Long segid, Long userid, String userAgentType) throws Exception{
		try{
			UserInfo user = userManager.getByUserid(userid);
			Date date = new Date();
			String content = article.getContent();
			String[] urls =KLGUtil.writeArticlemFile(content, date, userAgentType);
			article.setFilePath(urls[0]);
			article.setFileName(urls[1]);
			article.setFileExt(urls[2]);
			article.setFileFlag(Constants.USER_AGENT_TYPE_HTML.equals(userAgentType)?Constants.FILE_CREATED_FLAG_HTML:Constants.FILE_CREATED_FLAG_WML);
			
			article.setUserId(userid);
			article.setNickname(user.getNickname());
			article.setCreateTime(date);
			article.setLastModifyTime(date);
			article.setLastThreadTime(date);
			article.setClickAmount(0);
			article.setElite(ConstantDB.UNELITE);
			article.setEnabled(ConstantDB.AVALIABLE);
			article.setReply(ConstantDB.REPLY);
			article.setSource(Constants.USER_AGENT_TYPE_WML.equalsIgnoreCase(userAgentType)? ConstantDB.USER_AGENT_TYPE_WML :ConstantDB.USER_AGENT_TYPE_HTML);
			article.setTop(ConstantDB.UNTOP);
			article = (Article) wikiDao.saveObject(article);
			
			WikiSegmentArticle wsa= new WikiSegmentArticle();
			wsa.setArticleid(article.getArticleid());
			wsa.setSegmentid(segid);
			wikiDao.saveObject(wsa);
			
			return article;
		}catch(Exception e){
			log.debug(e);
			throw e;
		}
	}
	/**
	 * 获得某文章的分类.enabed or not都选出
	 * @param page
	 * @param userid 当前登录系统的用户，用于分页的PAGESIZE
	 * @param articleid 文章ID
	 */
	public void getPaginationByArticle(PaginationSupport page, Long userid, Long articleid) throws Exception{
		try{
			page.setClazz(ArticleReply.class);
			
			String queryString = "from ArticleReply t where t.articleid = " + articleid;
			queryString += " order by createTime asc";
			page.setQueryString(queryString);
			wikiDao.getPagination(page);
		}catch(Exception e){
			log.debug(e);
			throw e;
		}
	}
}
